package com.thinkgem.jeesite.modules.sys.listener;

import javax.servlet.ServletContext;

import com.thinkgem.jeesite.common.config.Global;
import org.springframework.web.context.WebApplicationContext;

import com.thinkgem.jeesite.modules.sys.service.SystemService;

import java.io.File;

public class WebContextListener extends org.springframework.web.context.ContextLoaderListener {
	
	@Override
	public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		if (!SystemService.printKeyLoadMessage()){
			return null;
		}
		String uploaddir = servletContext.getRealPath("") + "upload/";
		String appdir = uploaddir + "app/";
		String logdir = uploaddir + "log/";
		String realtimedir = uploaddir + "realtime/";
		String multimediadir = appdir + "multimedia/";
		String subdir = appdir + "sub/";
		String titledir = appdir + "title/";
		String imagedir = multimediadir + "image/";
		String videodir = multimediadir + "video/";
		try{
			File dir = new File(uploaddir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(appdir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(logdir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(realtimedir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(multimediadir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(subdir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(titledir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(imagedir);
			if(!dir.exists()){
				dir.mkdirs();
			}
			dir = new File(videodir);
			if(!dir.exists()){
				dir.mkdirs();
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return super.initWebApplicationContext(servletContext);
	}
}
